package com.soft.line.service.impl;

import com.github.pagehelper.PageInfo;
import com.soft.line.entity.CertificateInfo;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.AttachmentInfoMapper;
import com.soft.line.mapper.CertificateInfoMapper;
import com.soft.line.service.CertificateInfoService;
import com.soft.line.util.pagehelper.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wu.xueqiao on 2020/5/8.
 * @version 0.1
 */
@Service
@Slf4j
public class CertificateInfoServiceImpl implements CertificateInfoService {

    @Autowired
    private CertificateInfoMapper certificateInfoMapper;
    @Autowired
    private AttachmentInfoMapper attachmentInfoMapper;

    @Override
    public List<CertificateInfo> getPersonalAll(String userName) {
        return certificateInfoMapper.getPersonalAll(userName);
    }

    @Override
    public  PageInfo<CertificateInfo> getAllCompanyCertificates(final Map<String, Object> map) {
        PageUtil.setPageParam(map);
        List<CertificateInfo> certificateInfos=certificateInfoMapper.getAllCompanyCertificates(map);
        PageInfo<CertificateInfo> beans = new PageInfo(certificateInfos);
        beans.setList(certificateInfos);
        return beans;
    }

    @Override
    public PageInfo<CertificateInfo> getAllCertificates(Map<String, Object> map) {
        PageUtil.setPageParam(map);
        List<CertificateInfo> certificateInfos=certificateInfoMapper.selectAll(map);
        PageInfo<CertificateInfo> beans = new PageInfo(certificateInfos);
        beans.setList(certificateInfos);
        return beans;
    }

    @Override
    public void delete(Map<String, Object> map) {
        certificateInfoMapper.delete(map);
        attachmentInfoMapper.delete("2",(Integer)map.get("id"));
    }

    @Override
    public int countByCId(Integer id, String cId) {
        return certificateInfoMapper.countCertificateByIdAndCId(id,cId);
    }

    @Override
    public void add(CertificateInfo certificateInfo) {
        String userName="admin";
        UserTokenDto userToken=HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        certificateInfo.setModifyPerson(userName);
        certificateInfoMapper.insert(certificateInfo);
    }

    @Override
    public void update(CertificateInfo certificateInfo) {
        String userName="admin";
        UserTokenDto userToken=HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        certificateInfo.setModifyPerson(userName);
        certificateInfo.setModifyTime(new Date());
        certificateInfoMapper.update(certificateInfo);
    }

    @Override
    public void insertRecord(List<CertificateInfo> list) {
        String userName="admin";
        UserTokenDto userToken=HttpRequestUtil.getUserToken();
        if(userToken!=null){
            userName=userToken.getUserInfo().getUserName();
        }
        for(CertificateInfo record:list){
            record.setModifyPerson(userName);
            record.setModifyTime(new Date());
            certificateInfoMapper.insertRecord(record);
        }
    }

    @Override
    public List<CertificateInfo> getAllByIDList(Map<String, Object> map) {
        List<CertificateInfo> list = certificateInfoMapper.getAllByIdList((List<Integer>) map.get("ids"));
        return list;
    }
}
